package com.dragon.www.haier.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.HaierDevices;
import com.dragon.www.haier.entity.HaierDevicesInfo;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;
import com.dragon.www.haier.view.TitleView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by liqp on 2016/10/27.
 */
public class SmartControlActivity extends Activity{

    private TitleView title;
    private LinearLayout ll_light;
    private LinearLayout ll_curtain;
    private LinearLayout ll_door;
    private LinearLayout ll_air;
    private LinearLayout ll_humidifier;
    private LinearLayout ll_music;
    List<HaierDevices> HaierDevicesList = new ArrayList<HaierDevices>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smartcontrol);
        initView();
        getHaierDevices();
        title.setTextBackAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ll_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (HaierDevicesList != null) {
                    List<HaierDevices> HaierLampsList = null;
                    for (int i = 0; i < HaierDevicesList.size(); i++) {
                        HaierDevices HaierDevices = HaierDevicesList.get(i);
                        HaierLampsList = new ArrayList<HaierDevices>();
                        if (HaierDevices.getdevicetype() == 1) {
                            HaierLampsList.add(HaierDevices);
                        }
                        HaierLampActivity.setDevices(HaierLampsList);
                    }
                    if (HaierLampsList != null) {
                        Intent intent = new Intent(SmartControlActivity.this, HaierLampActivity.class);
                        intent.putExtra("list", (Serializable) HaierLampsList);
                        startActivity(intent);
                    } else{
                        Toast.makeText(SmartControlActivity.this, "无灯光设备" , Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

    }

    public void initView() {
        title = (TitleView) findViewById(R.id.item_title);
        ll_light = (LinearLayout) findViewById(R.id.ll_light);
        ll_curtain = (LinearLayout) findViewById(R.id.ll_curtain);
        ll_door = (LinearLayout) findViewById(R.id.ll_door);
        ll_air = (LinearLayout) findViewById(R.id.ll_air);
        ll_humidifier = (LinearLayout) findViewById(R.id.ll_humidifier);
        ll_music = (LinearLayout) findViewById(R.id.ll_music);
    }


    public void getHaierDevices() {
        NetHelper.getHaierDevices(this, new NetUtils.NetCallBack<HaierDevicesInfo>() {
            @Override
            public void success(HaierDevicesInfo rspData) {
                if (rspData.getError() == 0) {
                    HaierDevices[] HaierDevices = rspData.getHaierDevices();
                    Collections.addAll(HaierDevicesList, HaierDevices);
                } else {
                    Toast.makeText(SmartControlActivity.this, "获取设备信息失败，原因" + rspData.getDescription(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failed(String msg) {
                Log.e("请求错误", msg);
                Toast.makeText(SmartControlActivity.this, "未获取到设备", Toast.LENGTH_SHORT).show();
            }
        }, HaierDevicesInfo.class);
    }
}
